package net.lab1024.sa.admin.module.flowable.controller;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import net.lab1024.sa.admin.module.flowable.domain.dto.FlowTaskDto;
import net.lab1024.sa.admin.module.flowable.domain.dto.FlowViewerDto;
import net.lab1024.sa.admin.module.flowable.domain.form.FlowableTaskForm;
import net.lab1024.sa.admin.module.flowable.domain.vo.FlowTaskVo;
import net.lab1024.sa.admin.module.flowable.service.IFlowTaskService;
import net.lab1024.sa.admin.module.flowable.service.IFlowableDeployService;
import net.lab1024.sa.base.common.annoation.NoNeedLogin;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/flowable/task")
public class FlowableTaskController {
    @Resource
    private IFlowableDeployService flowableDeployService;

    @Resource
    private IFlowTaskService flowTaskService;

    @NoNeedLogin
    @PostMapping("/start/{procDefId}")
    public ResponseDTO<Object> start(@PathVariable(value = "procDefId") String procDefId,
                                     @RequestBody Map<String, Object> variables) {
        return flowableDeployService.startProcessInstanceById(procDefId, variables);

    }

    @Operation(summary = "分页查询 @author 胡克")
    @PostMapping("/query")
    @NoNeedLogin
    public ResponseDTO<PageResult<FlowTaskDto>> query(@RequestBody @Valid FlowableTaskForm queryForm) {
        return flowTaskService.query(queryForm);
    }

    @GetMapping("/cancel/{processInstanceId}")
    public ResponseDTO<Object> cancel(@PathVariable(value = "processInstanceId") String processInstanceId) {
        return flowTaskService.stopProcess(processInstanceId);
    }

    @GetMapping("/delete/{processInstanceId}")
    public ResponseDTO<Object> delete(@PathVariable(value = "processInstanceId") String processInstanceId) {
        FlowTaskVo flowTaskVo = new FlowTaskVo();
        flowTaskVo.setTaskId(processInstanceId);
        return flowTaskService.deleteTask(flowTaskVo);
    }

    @GetMapping("/viewer/{procInsId}")
    public ResponseDTO<List<FlowViewerDto>> getFlowViewer(@PathVariable(value = "procInsId") String procInsId) {
        return flowTaskService.getFlowViewer(procInsId);
    }

    @GetMapping("getRecord/{procInsId}")
    public ResponseDTO<List<FlowTaskDto>> getRecord(@PathVariable(value = "procInsId") String procInsId) {
        return ResponseDTO.ok(flowTaskService.flowRecord(procInsId));
    }

}
